package com.fenrirtec.aepp.portal.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ForwardControlInterceptor extends AbstractInterceptor {
	
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();

		HttpServletRequest req = ServletActionContext.getRequest();
		String path = req.getRequestURI().substring(7);
		String queryString = req.getQueryString();
		if (queryString == null) {
		    queryString = "";
		}
		String realPath = path + (StringUtils.isNotEmpty(queryString) ? "?" : "") + queryString;
		if (realPath.toLowerCase().startsWith("/forward/") && realPath.indexOf("/forward/login") < 0 && realPath.indexOf("/forward/auth") < 0 && realPath.indexOf("/forward/logout") < 0) {
		    session.setAttribute("prePage", realPath.replaceFirst("/forward/", ""));
		}
		
		return invocation.invoke();
	}
}
